﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDShop.Entities;
using BDShop.Business.Cache;
using BDShop.Models;
using BDShop.Utilities.Managers;
using BDShop.Resources;
using BDShop.Business;
using PagedList;
using BDShop.Configuration;

namespace BDShop.Controllers
{
    public class TagController : Controller
    {
        //
        // GET: /Tag/

        public ActionResult Index(int tagid)
        {
            //Get category detail
            Tag tag = TagCache.GetByID(tagid);
            TagModels tagModel = new TagModels();
            tagModel.ID = tag.ID;
            tagModel.Name = tag.Name;

            return View(tagModel);
        }

        public ActionResult ProductList(int tagID,string tagName, int sortOrder = 0, int? page = 1, string price = "All", string currentFilter = "")
        {
            int pageSize = BDShopConfiguration.Web.pageSize;
            ViewBag.Url = UrlManager.ProductOfTagUrl(tagID, tagName);
            //Get value for sort ddl
            List<SelectListItem> lstSort = new List<SelectListItem>();
            SelectListItem itemDefault = new SelectListItem() { Text = FO.macdinh, Value = "0", Selected = (0 == sortOrder) ? true : false };
            SelectListItem itemNameAZ = new SelectListItem() { Text = FO.tensanphamaz, Value = "1", Selected = (1 == sortOrder) ? true : false };
            SelectListItem itemNameZA = new SelectListItem() { Text = FO.tensanphamza, Value = "2", Selected = (2 == sortOrder) ? true : false };
            SelectListItem itemPriceTC = new SelectListItem() { Text = FO.giathapcao, Value = "3", Selected = (3 == sortOrder) ? true : false };
            SelectListItem itemPriceCT = new SelectListItem() { Text = FO.giacaothap, Value = "4", Selected = (4 == sortOrder) ? true : false };
            SelectListItem itemRateH = new SelectListItem() { Text = FO.binhchoncaonhat, Value = "5", Selected = (5 == sortOrder) ? true : false };
            SelectListItem itemRateL = new SelectListItem() { Text = FO.binhchonthapnhat, Value = "6", Selected = (6 == sortOrder) ? true : false };
            IEnumerable<SelectListItem> eItem = new SelectListItem[] { itemDefault, itemNameAZ, itemNameZA, itemPriceTC, itemPriceCT, itemRateH, itemRateL };
            lstSort.AddRange(eItem);
            ViewBag.Sort = lstSort;

            ViewBag.page = page;
            ViewBag.sortOrder = sortOrder;
            ViewBag.Price = price;

            List<ProductCacheComplex> lstProduct = new List<ProductCacheComplex>();
            //get product of tag
            List<ProductTag> lstProductOfTag = ProductTagService.GetListProductByTag(tagID);
            //Get Product Of Tag
            lstProduct = lstProductOfTag.Join(ProductCache.Instance.Get(), pc => pc.ProductID, p => p.ID, (pc, p) => p).Distinct().ToList();

            switch (sortOrder)
            {
                case 1:
                    lstProduct = lstProduct.OrderBy(p => p.Name).ToList();
                    break;
                case 2:
                    lstProduct = lstProduct.OrderByDescending(p => p.Name).ToList();
                    break;
                case 3:
                    lstProduct = lstProduct.OrderBy(p => p.Price).ToList();
                    break;
                case 4:
                    lstProduct = lstProduct.OrderByDescending(p => p.Price).ToList();
                    break;
                case 5:
                    lstProduct = lstProduct.OrderByDescending(p => Math.Ceiling((float)p.Rating / p.Rater)).ToList();
                    break;
                case 6:
                    lstProduct = lstProduct.OrderBy(p => Math.Ceiling((float)p.Rating / p.Rater)).ToList();
                    break;
                default:
                    lstProduct = lstProduct.OrderByDescending(p => p.DateCreated).ToList();
                    break;
            }

            //Filter by price
            List<ProductCacheComplex> lstProductResult = new List<ProductCacheComplex>();
            if (price == "All")
            {
                lstProductResult = lstProduct;
            }
            else
            {
                string[] lstPrice = price.Split('-');
                foreach (var s in lstPrice)
                {
                    switch (s)
                    {
                        case "0":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price < 100000)).ToList();
                            break;
                        case "1":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 100000 && p.Price <= 500000)).ToList();
                            break;
                        case "2":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 500000 && p.Price <= 1000000)).ToList();
                            break;
                        case "3":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 1000000)).ToList();
                            break;
                    }
                }
            }

            //Paging
            int pageNumber = (page ?? 1);

            return PartialView("Partial/_ProductList", lstProductResult.ToPagedList(pageNumber, pageSize));
        }
    }
}
